package org.azmat.demoredis.RedisTemplate.RedisSQL;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDao {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 存储字符串类型的key-value
    public void setValue(String key, String value){
        stringRedisTemplate.opsForValue().set(key, value);
    }


    // 通过键获取字符串类型的值
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    // 查找所有的书
    public List<Book> findAll(String id){
        return (List<Book>) redisTemplate.opsForValue().get(id);
    }

    // 保护书
    public void savaBooks(Object key,List<Book> value){
        redisTemplate.opsForValue().set(key,value);
    }

    // 通过id号查找一本书
    public Book findById(String id){
        return (Book) redisTemplate.opsForValue().get(String.valueOf(id));
    }

    // 保存书
    public void savaBook(Book book){
        redisTemplate.opsForValue().set(String.valueOf(book.getId()),book);
    }

    // 根据id删除书
    public void deleteBook(int id){
        redisTemplate.opsForValue().getOperations().delete(String.valueOf(id));
    }
}
